home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / jpl_c.zip / ATOL.C < prev    next >
Text File  |  1986-05-18  |  1KB  |  56 lines

  1. /* 1.2  01-08-86                        (atol.c)
  2. /************************************************************************
  3.  *            Robert C. Tausworthe                *
  4.  *            Jet Propulsion Laboratory            *
  5.  *            Pasadena, CA 91009        1986        *
  6.  ************************************************************************/
  7.  
  8. #include "defs.h"
  9. #include "stdtyp.h"
  10.  
  11. /************************************************************************/
  12.     long
  13. atol(s)        /* return long value of converted ascii string s.    */
  14.  
  15. /*----------------------------------------------------------------------*/
  16. STRING s;
  17. {
  18.     long i;
  19.     STRING astol();
  20.  
  21.     astol(s, &i);
  22.     return i;
  23. }
  24.  
  25. /************************************************************************/
  26.     STRING
  27. astol(s, val)        /* convert string s to long value and return
  28.                pointer to next character not converted    */
  29. /*----------------------------------------------------------------------*/
  30. STRING s;
  31. long *val;
  32. {
  33.     long v;
  34.     BOOL minus;
  35.     int c, base;
  36.     METACHAR tobase();
  37.  
  38.     while (isspace(*s))
  39.         s++;
  40.     if ((minus = (*s IS '-')) OR (*s IS '+'))
  41.         s++;
  42.     if (*s IS '0')
  43.         if (tolower(*(++s)) IS 'x')
  44.         {    base = 16;
  45.             s++;
  46.         }
  47.         else
  48.             base = 8;
  49.     else
  50.         base = 10;
  51.     for (v = 0; (c = tobase(*s, base)) >= 0; s++)
  52.         v = v * base + c;
  53.     *val = (minus ? -v : v);
  54.     return s;
  55. }
  56.